package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;

import cn.hutool.core.text.CharSequenceUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleService;

import com.zzyl.vo.AlertRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlertRuleServiceimpl implements AlertRuleService {
    @Autowired
    private AlertRuleMapper alertRuleMapper;

    /**
     * 新增告警规则
     *
     * @param alertRuleDto
     */
    @Override
    public void createAlertRule(AlertRuleDto alertRuleDto) {
        AlertRule bean = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRuleMapper.createAlertRule(bean);
    }

    /**
     * 分页查询告警规则
     *
     * @param
     * @return
     */
    @Override
    public PageResponse<AlertRuleVo> getPage(Integer pageNum, Integer pageSize, String alertRuleName, String functionId, String productKey) {
        PageHelper.startPage(pageNum, pageSize);
        Page<AlertRuleVo> pagef = alertRuleMapper.getpage(alertRuleName, functionId, productKey);
        pagef.getResult().forEach(item -> {
            String format = CharSequenceUtil.format("{}{}{},持续{}个周期报警", item.getFunctionName(), item.getOperator(), item.getValue(), item.getDuration());
            item.setRules(format);
        });
        return PageResponse.of(pagef, AlertRuleVo.class);
    }

    /**
     * 根据id查询告警规则
     * @param id
     * @return
     */
    @Override
    public AlertRuleVo read(Long id) {
        AlertRuleVo alertRuleVo = alertRuleMapper.read(id);
        return alertRuleVo;
    }

    /**
     * 更新告警规则
     * @param id
     * @param alertRuleDto
     */
    @Override
    public void update(Long id, AlertRuleDto alertRuleDto) {
        AlertRule alertRule = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRuleMapper.update(alertRule);
    }

    /**
     * 根据id删除告警规则
     * @param id
     */
    @Override
    public void delete(Long id) {
        alertRuleMapper.delete(id);
    }

    /**
     * 启用-禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        alertRuleMapper.updateStatus(id, status);
    }
}
